﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace EnglishRegister.Entities
{
    public class EnglishRegisterContext : DataContext
    {
        private static readonly string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["EnglishCourseEntities"] != null ?
                                                                              System.Configuration.ConfigurationManager.ConnectionStrings["EnglishCourseEntities"].ConnectionString : null;
        private bool isDispose = false;
        public bool IsDispose
        {
            get
            {
                return this.isDispose;
            }
        }

        #region **Contructor**
        public EnglishRegisterContext(string strConnection)
            : base(strConnection)
        {

        }

        public EnglishRegisterContext()
            : base(connectionString)
        {

        }
        #endregion

        #region **Functions**
        public void Insert<T>(T item) where T : class
        {
            try
            {
                Table<T> table = this.GetTable<T>();
                table.InsertOnSubmit(item as T);
                this.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public void Delete<T>(T item) where T : class
        {
            try
            {
                Table<T> table = this.GetTable<T>();
                table.Attach(item as T);
                table.DeleteOnSubmit(item as T);
                this.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void Update<T>(T item) where T : class
        {
            try
            {
                this.GetTable<T>().Attach(item as T, true);
                this.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        #endregion

        #region **Override**
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            this.isDispose = disposing;
        }
        #endregion
    }
}
